﻿//
// Created by work on 24-6-6.
//

#include "CRtModelSigBase.h"

std::map<CRtModelSigBase::EBaseSigCol, QString> CRtModelSigBase::baseSigColToStringMap = {
    {CRtModelSigBase::EModel_name, "model_name"},
    {CRtModelSigBase::EUser,"userid"},
    {CRtModelSigBase::EName,"name"},
    {CRtModelSigBase::EDescr,"descr"},
    {CRtModelSigBase::ESn,"sn"},
    {CRtModelSigBase::EGrouping,"groupings"},
    {CRtModelSigBase::EConfigs,"configs"}
};

std::map<CRtModelSigBase::EBaseSigCol, QString> CRtModelSigBase::baseColFieldsNameMap = {
    {EBaseSigCol::EModel_name, "模型名字"},
    {EBaseSigCol::EUser,"所属用户"},
    {EBaseSigCol::EName,"信号名称"},
    {EBaseSigCol::EDescr,"信号描述"},
    {EBaseSigCol::ESn,"排序"},
    {EBaseSigCol::EGrouping,"分组"},
    {EBaseSigCol::EConfigs,"详细配置"},
    };

std::map<QString, CRtModelSigBase::EBaseSigCol> CRtModelSigBase::nameToFieldsColMap = {
    {"模型名字", EBaseSigCol::EModel_name},
    {"所属用户", EBaseSigCol::EUser},
    {"信号名称", EBaseSigCol::EName},
    {"信号描述", EBaseSigCol::EDescr},
    {"排序", EBaseSigCol::ESn},
    {"分组", EBaseSigCol::EGrouping},
    {"详细配置", EBaseSigCol::EConfigs}
};

CRtModelSigBase::CRtModelSigBase(int colsize, QObject *parent):IModel(colsize, parent) {
    m_enPluginEdit.insert(baseSigColFieldName(EBaseSigCol::EConfigs), true);
    m_fieldPluginMap.insert(baseSigColFieldName(EBaseSigCol::EConfigs), "configeditplugin");
}

QString CRtModelSigBase::getFieldNameByCol(int col) {
    if(col < 0 || col > (SIGBASE_COL_SIZE - 1))
        return "";
    return baseSigColFieldName(static_cast<EBaseSigCol>(col));
}

QVariant CRtModelSigBase::getFieldValueByFielddName(const QString &name) {
    if(baseSigColFieldName(EBaseSigCol::EModel_name) == name) {
        return model_name;
    } else if(baseSigColFieldName(EBaseSigCol::EUser) == name) {
        return user;
    } else if(baseSigColFieldName(EBaseSigCol::EName) == name) {
        return this->name;
    } else if(baseSigColFieldName(EBaseSigCol::EDescr) == name) {
        return descr;
    } else if(baseSigColFieldName(EBaseSigCol::ESn) == name) {
        return sn;
    } else if(baseSigColFieldName(EBaseSigCol::EGrouping) == name) {
        return grouping;
    } else if(baseSigColFieldName(EBaseSigCol::EConfigs) == name) {
        return QString(QJsonDocument(configs).toJson());
    }
    return "未知数据,获取错误";
}

void CRtModelSigBase::setFieldValueByCol(int col, const QVariant &value) {
    if(col < EBaseSigCol::EDescr || col > (SIGBASE_COL_SIZE - 1))
        return;

    if(EBaseSigCol::EDescr == col) {
        descr = value.toString();
    } else if(EBaseSigCol::ESn == col) {
        sn = value.toInt();
    } else if(EBaseSigCol::EGrouping == col) {
        grouping = value.toString();
    } else if(EBaseSigCol::EGrouping == col) {
        QJsonParseError parseError;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(value.toString().toUtf8(), &parseError);
        if (parseError.error != QJsonParseError::NoError) {
            return;
        }
        configs = jsonDoc.object();
    }
}

QJsonObject CRtModelSigBase::toJson() {
    QJsonObject obj;
    obj[baseSigFieldName(EBaseSigCol::EModel_name)] = model_name;
    obj[baseSigFieldName(EBaseSigCol::EUser)] = user;
    obj[baseSigFieldName(EBaseSigCol::EName)] = name;
    obj[baseSigFieldName(EBaseSigCol::EDescr)] = descr;
    obj[baseSigFieldName(EBaseSigCol::ESn)] = sn;
    obj[baseSigFieldName(EBaseSigCol::EGrouping)] = grouping;
    obj[baseSigFieldName(EBaseSigCol::EConfigs)] = configs;
    return obj;
}

void CRtModelSigBase::fromJson(const QJsonObject &obj) {
    model_name = obj[baseSigFieldName(EBaseSigCol::EModel_name)].toString();
    user = obj[baseSigFieldName(EBaseSigCol::EUser)].toString();
    name = obj[baseSigFieldName(EBaseSigCol::EName)].toString();
    descr = obj[baseSigFieldName(EBaseSigCol::EDescr)].toString();
    sn = obj[baseSigFieldName(EBaseSigCol::ESn)].toInt();
    grouping = obj[baseSigFieldName(EBaseSigCol::EGrouping)].toString();
    configs = obj[baseSigFieldName(EBaseSigCol::EGrouping)].toObject();
}

QSharedPointer<IModel> CRtModelSigBase::createEmpty()
{

}
